//
//  GridListView.swift
//  BasicTools
//
//  Created by ikang on 2025/8/25.
//

import SwiftUI

/**
 * 尺寸：
 * - 固定尺寸.fixed()
 * - 灵活尺寸：.flexible(minimum: maximum:),默认会填充可用空间
 * - 自适应尺寸：.adaptive(minimum)
 */


public struct ListItemView: View {
    @State var listModel: GridListModel
    var tapItem: ((_ index: Int) -> Void)?
    
    public init(listModel: GridListModel, tapItem: ((_ index: Int) -> Void)? = nil) {
        self.listModel = listModel
        self.tapItem = tapItem
    }
    
    public var body: some View {
        ScrollView(.horizontal) { 
            LazyHStack(alignment: .center, spacing: listModel.gridHSpace) { 
                ForEach(0..<listModel.itemArr.count, id: \.self) { index in
                    @Bindable var itemModel: GridItemModel = listModel.itemArr[index]
                    
                    GridItemView(itemModel: itemModel, currentIndex: index)
                        .environment(listModel)
                        .onTapGesture {
                            if listModel.isCanSelect {
                                if listModel.isCanCancelSelect, listModel.selectIndex == index {
                                    listModel.selectIndex = -1
                                }else {
                                    listModel.selectIndex = index
                                }
                            }
                            
                            if let tapItem { tapItem(index) }
                        }
                }
            }
        }
        .bt.viewBorder(cornerRadius: listModel.gridCornerRadius, 
                       backgroundColor: listModel.backgroundColor, 
                       insets: listModel.gridPadding)
        
        
//        LazyVGrid(columns: Array(repeating: GridItem(.flexible(), spacing: listModel.gridHSpace), count: listModel.lineCount), spacing: listModel.gridVSpace) { 
//            
//        }
//        .bt.viewBorder(cornerRadius: listModel.gridCornerRadius, 
//                       backgroundColor: listModel.backgroundColor, 
//                       insets: listModel.gridPadding)
    }
}

//#Preview {
//    GridListView()
//}

